//
//  TSBaseViewController.m
//  chart
//
//  Created by xu on 15/7/21.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSBaseViewController.h"
#import <objc/runtime.h>
#import "TSHomeViewController.h"
#import "TSLoginViewController.h"

@implementation TSBaseViewController

static const char *buttonActionBlockKey;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.modalPresentationCapturesStatusBarAppearance = YES;
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.view.backgroundColor = RGB(239, 240, 244);
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    if (![self isRootViewController]) {
        __weak typeof(self) weakSelf = self;
        [self leftBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"nav_back"] barHltImage:nil action:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }
    else{
        WS(weakSelf);
        [self leftBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"nav_back"] barHltImage:nil action:^{
            if (!weakSelf) {
                return;
            }
            
            [weakSelf showHomeViewController];
        }];
    }
}

- (void)showHomeViewController
{
    TSLoginViewController *login_vc = [[TSLoginViewController alloc] init];
    TSHomeViewController *home_vc = [[TSHomeViewController alloc] init];
    TSBaseNavigationController *nav_vc = [[TSBaseNavigationController alloc] initWithRootViewController:login_vc];
    [self presentViewController:nav_vc animated:YES completion:^{
        [login_vc.navigationController pushViewController:home_vc animated:YES];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"barTag:%ld", (long)self.tabBarItem.tag);
    
    self.navigationController.navigationBarHidden = NO;
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

- (BOOL)isRootViewController {
    
    return [self.navigationController.viewControllers firstObject] == self;
}

#pragma mark - Customerise NavBar Item

- (UIButton *)leftBarButtonWithTitle:(NSString *)title
                         barNorImage:(UIImage *)norImage
                         barHltImage:(UIImage *)hltImage
                              action:(void(^)(void))actionBlock{
    
    UIButton *button = [self buttonWithTitle:title buttonNorImage:norImage buttonHltImage:hltImage];
    objc_setAssociatedObject(button, &buttonActionBlockKey, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barButtonItem;
    
    return button;
}

- (UIButton *)rightBarButtonWithTitle:(NSString *)title
                          barNorImage:(UIImage *)norImage
                          barHltImage:(UIImage *)hltImage
                               action:(void(^)(void))actionBlock{
    
    UIButton *button = [self buttonWithTitle:title buttonNorImage:norImage buttonHltImage:hltImage];
    objc_setAssociatedObject(button, &buttonActionBlockKey, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = barButtonItem;
    
    return button;
}

- (UIButton *)buttonWithTitle:(NSString *)title buttonNorImage:(UIImage *)norImage buttonHltImage:(UIImage *)hltImage{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:norImage forState:UIControlStateNormal];
    [button setImage:hltImage forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(actionButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    button.backgroundColor = [UIColor clearColor];
    button.titleLabel.font = FONT(12);
    [button sizeToFit];
    
    return button;
}

- (void)actionButtonClicked:(UIButton *)sender{
    
    void (^actionBlock) (void) = objc_getAssociatedObject(sender, &buttonActionBlockKey);
    if (actionBlock) {
        actionBlock();
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (self.navigationController.viewControllers.count == 1) {
        return NO;
    }
    else{
        return YES;
    }
}

@end
